package io.gitee.horizon007.webspider.provider;

import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Connection;
import org.jsoup.helper.HttpConnection;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

@Setter
@Accessors(chain = true)
public class WebProvider implements DocumentProvider {

    public static final Map<String, String> DEFAULT_HEADERS = Map.of(
            "Accept", "text/html",
            "Accept-Encoding", "gzip, deflate, br",
            "Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"
    );

    public static final String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.52";

    private int timeout = (int) TimeUnit.SECONDS.toMillis(30L);
    private String userAgent = DEFAULT_USER_AGENT;
    private Map<String, String> headers = DEFAULT_HEADERS;
    private Map<String, String> cookies;
    private Consumer<Connection> customizer;


    private final URL url;

    public WebProvider(String url) throws MalformedURLException {
        this(new URL(url));
    }

    public WebProvider(URL url) {
        this.url = url;
    }

    @Override
    public Document getDocument() throws IOException {
        Connection connect = HttpConnection.connect(url);
        connect.timeout(timeout);
        if (StringUtils.isNotEmpty(userAgent)) {
            connect.userAgent(userAgent);
        }
        if (MapUtils.isNotEmpty(headers)) {
            connect.headers(headers);
        }
        if (MapUtils.isNotEmpty(cookies)) {
            connect.cookies(cookies);
        }
        if (customizer != null) {
            customizer.accept(connect);
        }
        return connect.get();
    }
}
